//
//  DevFormTextField.swift
//  DevFormView
//
//  Created by dev on 15/9/17.
//  Copyright (c) 2015年 devilved. All rights reserved.
//
import Foundation
import UIKit

class DevFormTextField: DevFormField {

    var textField:UITextField?
    
    var textFont:UIFont?
    
    var textAlignment:NSTextAlignment?
    
    var clearButtonMode:UITextFieldViewMode?
    
    var keyboardType:UIKeyboardType?
    
    var returnKeyType:UIReturnKeyType?
    
    var inputView:UIView?
    
    var textColor:UIColor?
    
    var placeholder:String?
    
    override func initDevFormFieldView() {
        super.initDevFormFieldView()
        self.textField = UITextField()
        let text:UITextField = self.textField!
        let main:UIView = self.view!
        let width:CGFloat = self.formTableView.frame.width
        let left:CGFloat = self.textLabel!.frame.origin.x + self.textLabel!.frame.width
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        
        text.delegate = self.formTableView
        text.text = self.defaultValue as? String
        text.frame = CGRectMake(left, (self.height - self.controlHeight) / 2, width - left - right, self.controlHeight)
        text.placeholder = self.placeholder
        
        if let font:UIFont = self.textFont {
            text.font = font
        }
        if let keyboardType = self.keyboardType {
            text.keyboardType = keyboardType
        }
        if let inputView = self.inputView {
            text.inputView = inputView
        }
        if let returnKeyType = self.returnKeyType {
            text.returnKeyType = returnKeyType
        }
        if let textColor = self.textColor {
            text.textColor = textColor
        }
        if let textAlignment = self.textAlignment {
            text.textAlignment = textAlignment
        } else {
            text.textAlignment = NSTextAlignment.Right
        }
        if let clearButtonMode = self.clearButtonMode {
            text.clearButtonMode = clearButtonMode
        } else {
            text.clearButtonMode = UITextFieldViewMode.WhileEditing
        }
        
        main.addSubview(text)
        
        text.addTarget(self, action: "onEditingDidBegin", forControlEvents: UIControlEvents.EditingDidBegin)
        text.addTarget(self, action: "onEditingChanged", forControlEvents: UIControlEvents.EditingChanged)
    }
    
    
    override func initConstraints() {
        super.initConstraints()
        let text:UITextField = self.textField!
        let main:UIView = self.view!
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        text.translatesAutoresizingMaskIntoConstraints = false

        main.addConstraint(NSLayoutConstraint(item: main, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: text, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: right))
        
        main.addConstraint(NSLayoutConstraint(item: text, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.textLabel!, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: text, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    }
    
    override func setFieldValue(value: AnyObject?) {
        super.setFieldValue(value)
        self.textField?.text = value as? String
    }
    
    override func click() {
        self.focus()
    }
    
    override func focus() {
        if self.textField!.canBecomeFirstResponder() {
            self.textField!.becomeFirstResponder()
        }
        super.focus()
    }
    func onEditingDidBegin() {
        super.focus()
    }
    
    func onEditingChanged() {
        self.setFieldValue(self.textField?.text)
    }
    
    override func getEditableView() -> UIView? {
        return self.textField
    }
}
